home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Communications / Networks / MailCheck 0.9x / drvr src / mmc_notify.c < prev    next >
Text File  |  1992-06-15  |  6KB  |  274 lines

  1. /*
  2.  *
  3.  * mac mail check network checking module
  4.  * by Aaron Wohl (aw0g+@andrew.cmu.edu) jul 1990
  5.  * Carnegie-Mellon University
  6.  * Pittsburgh, PA 15213-3890
  7.  * (412)-268-5032
  8.  *
  9.  */
  10.  
  11. #include <string.h>
  12. #include "mmc_core.h"
  13. #include "mmc_notify.h"
  14. #include "mmc_core_private.h"
  15. #include "mmc_os_preserve.h"
  16. #include "mmc_config.h"
  17.  
  18. static note_vars nv;
  19.  
  20. /*
  21.  * rememer a private copy of text for use by the notification mgr
  22.  */
  23. LDEF(char *remember(char *text))
  24. {
  25.     static char buf[256];
  26.     int len=strlen(text);
  27.     char *dst=buf+1;
  28.     len=imax(0,imin(250,len));
  29.     strcpy(buf+1,"* ");
  30.     dst+=strlen(dst);
  31.     if(len!=0) {
  32.         memcpy(dst,text,len);
  33.         dst+=len;
  34.         *dst= 0;
  35.     }
  36.     buf[0]=strlen(buf+1);
  37.     return buf;
  38. }
  39.  
  40. /*
  41.  * get and detach a resource if there is space
  42.  */
  43. LDEF(char **get_and_detach(long reskind,int resid,int *goterror))
  44. {
  45.     char **result;
  46.     long res_size;
  47.     result=GetResource(reskind,resid);
  48.     SetResLoad(TRUE);
  49.     if(result==0) {
  50.           *goterror= TRUE;
  51.             strcpy(mmc_gl.cs.mmc_text_state,
  52.                 "New MailCheck configuration set, but can't find your sound resource.");
  53.         return 0;
  54.     }
  55.     res_size=SizeResource(result);
  56. #ifdef XXXX
  57. Multifinder seems to expand the system heap when it wants so this isn't accurate.
  58.     if(res_size>40) {
  59.         long max_free=MaxBlock();
  60.         if((max_free-res_size)<SYS_RESERVE) {
  61.           *goterror= TRUE;
  62.           ReleaseResource(result);
  63.             strcpy(mmc_gl.cs.mmc_text_state,
  64.                 "New MailCheck configuration set, but not enough space for sound in system heap.");
  65.           return 0;    
  66.         }
  67.     }
  68. #endif
  69.     LoadResource(result);
  70.     if(ResError()!=0) {
  71.         ReleaseResource(result);
  72.         return 0;
  73.     }
  74.     DetachResource(result);
  75.     return result;
  76. }
  77.  
  78. /*
  79.  * get and detach a resource into the system zone if there is space
  80.  */
  81. LDEF(char **get_res_into_sys(long reskind,int resid,int *goterror))
  82. {
  83.     register char **result;
  84.     result=(char **)OSP_protected_call(OSP_sys|OSP_noload,get_and_detach,reskind,resid,goterror);
  85.     return result;
  86. }
  87.  
  88. LDEF(void mmc_uninit_sound(void))
  89. {
  90.     if(nv.snd_handle!=0) {
  91.         DisposHandle(nv.snd_handle);
  92.         nv.snd_handle=0;
  93.     }
  94. }
  95.  
  96. int mmc_note_sound_init(int new_sound_res_num)
  97. {
  98.     int got_error=0;
  99.     if(nv.sicn_handle[MMC_note_mail]==0)
  100.       nv.sicn_handle[MMC_note_mail]=get_res_into_sys('SICN',-4064,&got_error);
  101.     if(nv.sicn_handle[MMC_note_error]==0)
  102.       nv.sicn_handle[MMC_note_error]=get_res_into_sys('SICN',-4063,&got_error);
  103.     if(new_sound_res_num!=mmc_gl.cs.mmc_sound_id) {
  104.       mmc_uninit_sound();
  105.       if(gFLSET(MCS_nsound)&&(nv.snd_handle==0))
  106.         nv.snd_handle=get_res_into_sys('snd ',new_sound_res_num,&got_error);
  107.     }
  108.     mmc_gl.cs.mmc_sound_id=new_sound_res_num;
  109.     return got_error;
  110. }
  111.  
  112. /*
  113.  * remove a notification event
  114.  */
  115. LDEF(pascal void notify_done_routine(struct NMRec *arec))
  116. {
  117.     if(arec==0)
  118.         DebugStr("\pMailCheck nremove NIL");
  119.     if(arec->nmRefCon==0)return;
  120.     NMRemove(arec);
  121.     arec->nmRefCon=0;
  122. }
  123.  
  124. /*
  125.  * install a notification event
  126.  */
  127. LDEF(int mmc_note_install(
  128.     char *text,
  129.     Handle sound,
  130.     Handle sicn,
  131.     struct NMRec *arec))
  132. {
  133.     int err;
  134.  
  135.     if(arec==0)
  136.         DebugStr("\pMailCheck ninstall NIL");
  137.     if(!gFLSET(MCS_nblink))
  138.         sicn=0;
  139.     if(!gFLSET(MCS_nsound)||(SdVolume==0))
  140.         sound=0;
  141.     if(gFLSET(MCS_nsound)&&(SdVolume==0))
  142.         SysBeep(30);            /*flash the title bar if can't speak*/
  143.  
  144.     if(arec->nmRefCon!=0) {
  145.         if((sound==0)&&(text==0)&&(sicn==arec->nmIcon))
  146.             return;            /*already doing this*/
  147.         notify_done_routine(arec);
  148.     }
  149.  
  150.     if((sound==0)&&(text==0)&&(sicn==0))
  151.         return;
  152.  
  153.     memset(arec,0,sizeof(*arec));
  154.     arec->qType=nmType;
  155.     arec->nmSound=sound;
  156.     arec->nmStr=(StringPtr)text;
  157.     arec->nmIcon=sicn;
  158.     if(sicn==0)
  159.         arec->nmResp= ((ProcPtr)¬ify_done_routine);
  160.     arec->nmRefCon=1;
  161.  
  162.     err=NMInstall(arec);
  163.     if(err!=0) {
  164.         arec->nmRefCon=0;
  165.         SysBeep(30);
  166.         return FALSE;
  167.     }
  168.     return TRUE;
  169. }
  170.  
  171. /*
  172.  * insert a notify alert into the system queue
  173.  */
  174. LDEF(void mmc_note_post(Handle sound))
  175. {
  176.     char *text=0;
  177.     if(sound==0)
  178.         sound=((Handle)-1);
  179.     if(!gFLSET(MCS_nsound))
  180.         sound=0;
  181.  
  182.     if(gFLSET(MCS_npost)) {
  183.         textify_error();
  184.         text=remember(mmc_gl.cs.mmc_text_state);
  185.     }
  186.  
  187.     mmc_note_install(text,sound,0L,&nv.post_note_rec);
  188. }
  189.  
  190. LDEF(void remind_error(void))
  191. {
  192.   mmc_note_install(0L,0L,
  193.     nv.sicn_handle[MMC_note_error],
  194.     &nv.blink_note_rec);
  195.   mmc_note_post(0);
  196.   set_event(EV_remind,remind_error,mmc_gl.cs.mmc_remind_time);
  197. }
  198.  
  199. LDEF(void remind_mail(void))
  200. {
  201.   mmc_note_install(0L,0L,
  202.     nv.sicn_handle[MMC_note_mail],
  203.     &nv.blink_note_rec);
  204.   mmc_note_post(nv.snd_handle);
  205.   set_event(EV_remind,remind_mail,mmc_gl.cs.mmc_remind_time);
  206. }
  207.  
  208. void set_event(int evnum,void (*dowhat)(),long dowhen)
  209. {
  210.     register mmc_event_pt anev= &mmc_gl.cps.ev[evnum];
  211.     anev->ev_time=dowhen+TickCount();
  212.     anev->ev_event=dowhat;
  213. }
  214.  
  215. /*
  216.  * uninitialize the notification system
  217.  */
  218. void mmc_note_done(void)
  219. {
  220.     notify_done_routine(&nv.post_note_rec);
  221.     notify_done_routine(&nv.blink_note_rec);
  222.     set_event(EV_remind,EV_NOTHING,0);
  223.     set_event(EV_error,EV_NOTHING,0);
  224. }
  225.  
  226. void mmc_note_new_error(void)
  227. {
  228.   if(mmc_gl.cps.ev[EV_remind].ev_event==remind_error)
  229.     return;
  230.   set_event(EV_remind,remind_error,0);
  231. }
  232.  
  233. void mmc_note_old_mail(void)
  234. {
  235.   if(mmc_gl.cps.ev[EV_remind].ev_event!=remind_mail)
  236.       set_event(EV_remind,remind_mail,MIN_REMIND);
  237. }
  238.  
  239. void mmc_note_no_error(void)
  240. {
  241.   mmc_gl.cs.mmc_last_err_kind=MCE_NOERRR;
  242.   if(mmc_gl.cps.ev[EV_remind].ev_event==remind_error)
  243.     mmc_note_done();
  244. }
  245.  
  246. void mmc_note_new_mail(void)
  247. {
  248.   set_event(EV_remind,remind_mail,0);
  249. }
  250.  
  251. void mmc_note_no_mail(void)
  252. {
  253.   if(mmc_gl.cps.ev[EV_remind].ev_event==remind_mail)
  254.     mmc_note_done();
  255. }
  256.  
  257. void mmc_note_init(void)
  258. {
  259.     memset(&nv,0,sizeof(nv));
  260. }
  261.  
  262. void mmc_note_uninit()
  263. {
  264.     register int i;
  265.     mmc_note_done();
  266.     for(i=0;i<MMC_note_kinds;i++)
  267.       if(nv.sicn_handle[i]!=0) {
  268.         DisposHandle(nv.sicn_handle[i]);
  269.         nv.sicn_handle[i]=0;
  270.       }
  271.     mmc_uninit_sound();
  272.     mmc_note_init();
  273. }
  274.